#!/usr/bin/env python
# -*- coding: utf-8 -*-

"""
微表情检测系统启动脚本
"""

import os
import sys
import logging
import argparse

def setup_logging():
    """设置日志"""
    logging.basicConfig(
        level=logging.DEBUG,
        format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
        handlers=[
            logging.StreamHandler(sys.stdout)
        ]
    )
    return logging.getLogger(__name__)

logger = setup_logging()

if __name__ == "__main__":
    logger.info("正在启动微表情检测系统...")
    
    parser = argparse.ArgumentParser(description="微表情检测系统")
    parser.add_argument("--port", type=int, default=5000, help="Web服务端口")
    parser.add_argument("--host", type=str, default="0.0.0.0", help="Web服务主机")
    parser.add_argument("--debug", action="store_true", help="启用调试模式")
    args = parser.parse_args()
    
    # 确保上传目录存在
    logger.info("确保上传目录存在")
    os.makedirs("uploads", exist_ok=True)
    
    try:
        logger.info("导入应用模块")
        from app import app, mock_init_app
        
        # 初始化模拟应用
        logger.info("初始化模拟应用")
        mock_init_app()
        
        logger.info(f"微表情检测系统已启动")
        logger.info(f"请访问 http://localhost:{args.port}")
        
        # 运行应用
        app.run(host=args.host, port=args.port, debug=args.debug)
    except ImportError as e:
        logger.error(f"导入模块失败: {e}")
        print(f"导入模块失败: {e}")
    except Exception as e:
        logger.error(f"启动应用失败: {e}")
        print(f"启动应用失败: {e}") 